﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNET4Study
{
    public static class LinqLamdaStudy
    {
        class Person
        {
            private int age;
            public int Age
            {
                get { return age; }
                set { age = value; }
            }

            private string name;
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
        }

        sealed class Student : Person
        {
            private string language;
            public string Language
            {
                get { return language; }
                set { language = value; }
            }
        }

        private static List<Student> students;
        private static List<Student> Students
        {
            get
            {
                if (students == null)
                {
                    students = new List<Student>() 
                    { 
                          new Student{ Name="YOUNG", Age=25, Language="Chinese"},
                          new Student{ Name="JESSIE", Age=21, Language="Scotland"},
                          new Student{ Name="KELLY", Age=18, Language="English"},
                          new Student{ Name="JUNE", Age=20, Language="English"},
                          new Student{ Name="ADRIAN", Age=22, Language="Italy"},
                          new Student{ Name="BRUCE", Age=17, Language="Scotland"},
                          new Student{ Name="BRANT", Age=30, Language="Germany"},
                          new Student{ Name="BEN", Age=25, Language="Chinese"}
                    };
                }
                return students;
            }
        }

        public static void Execute()
        {
            Select();
            First();
            ConvertAll();
            Sort();
        }

        private static void ConvertAll()
        {
            List<Person> personList = Students.ConvertAll<Person>(x => (Person)x);
            Console.WriteLine(personList.Count);
        }

        private static void Sort()
        {
            //Console.WriteLine("==以下开始显示排序后的内容（缺省排序规则）======================");
            //Students.Sort();
            //foreach (Student s in Students)
            //{
            //    Console.WriteLine(s.Name);
            //}
            Console.WriteLine("==以下开始显示排序后的内容（自定义排序规则之学生名长度）======================");
            Students.Sort((x, y) =>
            {
                int result = 0;
                int xLenght = x.Name.Length;
                int yLenght = y.Name.Length;

                result = xLenght - yLenght;
                return result;
            });

            foreach (Student s in Students)
            {
                Console.WriteLine(s.Name);
            }

            Console.WriteLine("==以下开始显示排序后的内容（自定义排序规则之学生名字母排序）======================");
            Students.Sort((x, y) =>
            {
                int result = 0;
                

                result = x.Name.CompareTo(y.Name);
                return result;
            });

            foreach (Student s in Students)
            {
                Console.WriteLine(s.Name);
            }
        }

        private static void Select()
        {
            /*
             所有 LINQ 查询操作都由以下三个不同的操作组成：
                1.获取数据源。
                2.创建查询。
                3.执行查询。
             */
            OnlySearch();

        }

        private static void OnlySearch()
        {
            //1.linq
            var enumList = from n in Students
                           where n.Age > 23
                           select n.Name;

            

            ////2.lamda
            //var enumList = Students.Where(s => s.Age > 23).Select(s => s.Name);

            foreach (var item in enumList)
            {
                Console.WriteLine(item);
            }


            //var query = from m in Students
            //            where 1 == 1
            //            select m;

            //query.ex
        }

        private static void First()
        {
            //List<Student> list = null;//new List<Student>();
            //Student s= list.FirstOrDefault();
            //Console.WriteLine(s.Name);

            List<KeyValuePair<string, List<string>>> kvpList = new List<KeyValuePair<string, List<string>>>();
            KeyValuePair<string, List<string>> kvp = kvpList.FirstOrDefault();
            Console.WriteLine(kvp.Value);
        }
    }
}
